<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-24
 * Time: 15:13
 * 余额支付
 */

namespace app\api\service;


use app\api\model\GoodsOrder;
use app\api\model\GoodsOrderContent;
use app\api\model\MoneyOrder;
use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\service\order\GoodsOrderSer;
use app\api\service\order\StoreOrderSer;
use app\lib\exception\ParameterException;

class MoneyPay extends Token
{
    // 支付number
    private $outTradeNo;

    // 订单金额
    private $price;

    // 订单类型
    private $type;

    // 用户ID
    private $user;

    // 订单
    private $order;

    function __construct($outTradeNo)
    {
        $this->outTradeNo = $outTradeNo;
        $this->user = User::get(self::getCurrentUid());
        $outTradeNo = explode("z",$this->outTradeNo);
        $this->type = $outTradeNo[0];
    }

    // 入口方法
    public function pay()
    {
        return $this->checkOrder();
    }

    // 判断订单信息
    private function checkOrder()
    {
        if($this->type == 2) {
            $this->order = StoreOrder::where('out_trade_no',$this->outTradeNo)->find();
            $this->price = $this->order['price'] - $this->order['coupon_price'];
        }elseif ($this->type == 3){
            $this->order = GoodsOrder::where('out_trade_no',$this->outTradeNo)->find();
            $this->price = $this->order['price'] - $this->order['coupon_price'];
            $this->judgeOrderContentNumber();
        } else {
            throw new ParameterException([
                'msg' => [
                    'order' => '此订单不能使用余额支付'
                ]
            ]);
        }
        return $this->judgeOrderPublic();
    }

    // 判断商品库存
    private function judgeOrderContentNumber()
    {
        $orderContent = GoodsOrderContent::with('goods')->where('order_id',$this->order['id'])->select()->toArray();
        if(count($orderContent) == 0) {
            throw new ParameterException([
                'msg' => [
                    'goods' => '订单为空'
                ]
            ]);
        }
        foreach ($orderContent as $k=>$v) {
            if($orderContent[$k]['number'] > $orderContent[$k]['goods']['number']) {
                throw new ParameterException([
                    'msg' => [
                        'goods' => $orderContent[$k]['goods']['name'].' 库存不足'
                    ]
                ]);
            }
            if($orderContent[$k]['goods']['up_down'] == 2) {
                throw new ParameterException([
                    'msg' => [
                        'goods' => $orderContent[$k]['goods']['name'].' 已下架'
                    ]
                ]);
            }
            if($orderContent[$k]['goods_sort_id'] == 9) {
                throw new ParameterException([
                    'msg' => [
                        'goods' => $orderContent[$k]['goods']['name'].' 不能使用余额支付'
                    ]
                ]);
            }
        }
    }


    // 判断会员订单
    private function judgeOrderPublic()
    {
        // 订单是否存在
        if(!$this->order) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单不存在,请重新选择订单'
                ]
            ]);
        }
        if($this->type == 2) {
            // 订单状态
            if($this->order['status'] != 3) {
                throw new ParameterException([
                    'msg' => [
                        'order' => '订单已经支付'
                    ]
                ]);
            }
        } else {
            // 订单状态
            if($this->order['status'] != 1) {
                throw new ParameterException([
                    'msg' => [
                        'order' => '订单已经支付'
                    ]
                ]);
            }
        }
        // 订单用户
        if($this->order['user_id'] != $this->user['id']) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单用户与此用户不一致!'
                ]
            ]);
        }
        return $this->judgeMoney();
    }

    // 判断余额
    private function judgeMoney()
    {
        if($this->user['money'] < $this->price) {
            throw new ParameterException([
                'msg' => [
                    'order' => '余额不足'
                ]
            ]);
        }
        return $this->updateOrder();
    }

    private function updateOrder()
    {
        $this->order['payment'] = 2;
        if($this->type == 2) {
            StoreOrder::where('out_trade_no',$this->outTradeNo)->update([
                'payment' => 2
            ]);
            $storeOrder = new StoreOrderSer($this->order);
            $status = $storeOrder->changeStatus();
        }elseif ($this->type == 3){
            GoodsOrder::where('out_trade_no',$this->outTradeNo)->update([
                'payment' => 2
            ]);
            $goodsOrder = new GoodsOrderSer($this->order);
            $status = $goodsOrder->changeStatus();
        }
        if($status) {
            return [
                'pay_type' => 2
            ];
        } else {
            throw new ParameterException([
                'msg' => [
                    'order' => '支付失败'
                ]
            ]);
        }
    }
}